package com.zzy.utils;

import com.zzy.annotatin.TableId;
import com.zzy.commons.DataType;

import java.lang.reflect.Field;
import java.sql.ResultSet;

public class MapperUtils {

    public static StringBuffer setFieldValue(StringBuffer buffer, Field field, Object o) throws IllegalAccessException {
        field.setAccessible(true);
        String typeName = field.getGenericType().getTypeName();
        if (typeName.equals(DataType.stringType) || typeName.equals(DataType.charType) || typeName.equals(DataType.CharType) || typeName.equals(DataType.dateType)){
            buffer.append("'");
            buffer.append(field.get(o));
            buffer.append("'");
        }else{
            buffer.append(field.get(o));
        }
        return buffer;
    }
    //设置key = value
    public static StringBuffer setFiledNameAndValue(StringBuffer buffer, Field field,Object o) throws IllegalAccessException {
        field.setAccessible(true);
        String typeName = field.getGenericType().getTypeName();
        if (typeName.equals(DataType.stringType) || typeName.equals(DataType.charType) || typeName.equals(DataType.CharType) || typeName.equals(DataType.dateType)){
            buffer.append(field.getName());
            buffer.append("=");
            if (field.get(o) == null){
                buffer.append("");
            }else {
                buffer.append("'");
                buffer.append(field.get(o));
                buffer.append("'");
            }
        }else{
            buffer.append(field.getName());
            buffer.append("=");
            if (field.get(o) == null){
                buffer.append("");
            }else {
                buffer.append(field.get(o));
            }
        }
        return buffer;
    }
    //得到主键搜索TableId
    public static Boolean isPrimaryKey(Field field){
        TableId annotation = field.getAnnotation(TableId.class);
        if (annotation != null){
            return true;
        }else {
            return false;
        }
    }
    //得到从数据库查询回来的值 根据不同类型分类
    public static Object getValue(Field field, ResultSet resultSet) throws Exception{
        field.setAccessible(true);
        Object result = null;
        String typeName = field.getGenericType().getTypeName();
        if (typeName.equals(DataType.stringType)){
            result = resultSet.getString(field.getName());
        }else if (typeName.equals(DataType.byteType) || typeName.equals(DataType.ByteType)){
            result = resultSet.getByte(field.getName());
        }else if (typeName.equals(DataType.shortType) || typeName.equals(DataType.ShortType)){
            result = resultSet.getShort(field.getName());
        }else if (typeName.equals(DataType.intType) || typeName.equals(DataType.IntegerType)){
            result = resultSet.getInt(field.getName());
        }else if (typeName.equals(DataType.LongType) || typeName.equals(DataType.longType)){
            result = resultSet.getLong(field.getName());
        }else if (typeName.equals(DataType.FloatType) || typeName.equals(DataType.floatType)){
            result = resultSet.getFloat(field.getName());
        }else if (typeName.equals(DataType.DoubleType) || typeName.equals(DataType.doubleType)){
            result = resultSet.getDouble(field.getName());
        }else if (typeName.equals(DataType.CharType) || typeName.equals(DataType.charType)){
            result = resultSet.getString(field.getName());
        }else if (typeName.equals(DataType.dateType)){
            result = resultSet.getDate(field.getName());
        }
        return result;
    }

}
